package com.kuiandjie.web;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.kuiandjie.entity.Employee;
import com.kuiandjie.entity.Goal;
import com.kuiandjie.entity.Log;
import com.kuiandjie.entity.Project;
import com.kuiandjie.entity.Task;
import com.kuiandjie.service.EmployeeService;
import com.kuiandjie.service.LogService;
import com.kuiandjie.service.TaskService;
import com.kuiandjie.util.DateUtil;

public class EditTaskServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String role = (String) session.getAttribute("role");
		TaskService ts = new TaskService();
		
		if("leader".equals(role) || "member".equals(role)) {
			String type = request.getMethod();
			if("POST".equals(type)) {
				response.setContentType("text/html");
				PrintWriter out = response.getWriter();
				int taskId = Integer.parseInt(request.getParameter("taskId"));
				
				Project curp = (Project) session.getAttribute("curp");
				Employee e = (Employee) session.getAttribute("employee");
				Goal curgoal = (Goal) session.getAttribute("curgoal");
				if("leader".equals(role)) {
					String taskName = request.getParameter("taskName");
					int empId = Integer.parseInt(request.getParameter("empId"));
					String beginTime = request.getParameter("beginTime");
					String endTime = request.getParameter("endTime");
					String state = request.getParameter("state");
					String level = request.getParameter("level");
					String desc = request.getParameter("desc");
					
					ts.editTaskByLeader(taskId, taskName, state, level, beginTime, endTime, desc, empId,DateUtil.getNow());
					Log log = new Log();
					log.setDesc("修改了任务<a href='task.jspx?gid="+ curgoal.getId() +"'>" + taskName + "</a>");
					log.setEmployeeId(e.getId());
					log.setProjectId(curp.getId());
					log.setTime(DateUtil.getNow());
					log.setName(e.getName());
					
					LogService ls = new LogService();
					ls.inserLog(log);
					
					request.setAttribute("msg", new Gson().toJson(log));
					request.getRequestDispatcher("msgflow.jspx").include(request, response);
				} else {
					String name = request.getParameter("taskName");
					String state = request.getParameter("state");
					int progress = Integer.parseInt(request.getParameter("progress"));
					ts.editTaskByMember(taskId, state, progress);
					Log log = new Log();
					if(progress == 100) {
						log.setDesc("任务<a href='task.jspx?gid="+ curgoal.getId() +"'>" + name + "</a>已完成");
					} else {
						log.setDesc("任务<a href='task.jspx?gid="+ curgoal.getId() +"'>" + name + "</a>已完成" + progress + "%");
					}
					
					log.setEmployeeId(e.getId());
					log.setProjectId(curp.getId());
					log.setTime(DateUtil.getNow());
					log.setName(e.getName());
					log.setPic(e.getPic());
					
					LogService ls = new LogService();
					ls.inserLog(log);
					
					request.setAttribute("msg", new Gson().toJson(log));
					request.getRequestDispatcher("msgflow.jspx").include(request, response);
				}
				out.print("1");
				out.flush();
				out.close();
			} else {
				String tid = request.getParameter("tid");
				if(tid == null || "".equals(tid)) {
					response.sendRedirect("task.jspx");
				} else {
					Project curp = (Project) session.getAttribute("curp");
					
					int curTaskId = Integer.parseInt(tid);
					Task task = null;
					if("leader".equals(role)) {
						
						task = ts.findTaskByTaskId(curTaskId);
						List<Employee> emList = getAllMember(curp.getId());
						
						request.setAttribute("emList", emList);
						request.setAttribute("task", task);
						
						request.getRequestDispatcher("WEB-INF/views/edittask.jsp").forward(request, response);
					} else {
						Employee em = (Employee) session.getAttribute("employee");
						task = ts.findTaskByTaskIdAndEmpId(curTaskId, em.getId());
						if(task != null) {
							request.setAttribute("task", task);
							
							request.getRequestDispatcher("WEB-INF/views/medittask.jsp").forward(request, response);
						}
					}
				}
			}
		} else {
			response.sendRedirect("projectdetail.jspx");
		}
	}
	
	private List<Employee> getAllMember(int proId) {
		EmployeeService es = new EmployeeService();
		return es.findAllMember(proId);
	}
}
